package com.ioidea.holland

import android.bluetooth.le.ScanFilter
import android.content.Intent
import android.os.ParcelUuid
import cn.com.heaton.blelibrary.ble.Ble
import cn.com.heaton.blelibrary.ble.Ble.InitCallback
import cn.com.heaton.blelibrary.ble.BleLog
import cn.com.heaton.blelibrary.ble.BleRssiDevice
import cn.com.heaton.blelibrary.ble.model.BleDevice
import cn.com.heaton.blelibrary.ble.model.BleFactory
import cn.com.heaton.blelibrary.ble.utils.UuidUtils
import com.ioidea.base.BaseApplication
import com.ioidea.ble.MyBleWrapperCallback
import com.ioidea.holland.api.PhoneBleApi
import com.ioidea.room.bean.JKChildDevDao
import com.scwang.smartrefresh.layout.SmartRefreshLayout
import com.scwang.smartrefresh.layout.footer.ClassicsFooter
import com.scwang.smartrefresh.layout.header.ClassicsHeader
import dagger.hilt.android.HiltAndroidApp
import java.util.UUID
import javax.inject.Inject

@HiltAndroidApp
class HollandApplication : BaseApplication() {
    @Inject
    lateinit var jkChildDevDao: JKChildDevDao
    override fun onCreate() {
        super.onCreate()
        initSmartRefreshLayout()
        initBle()
        PhoneBleApi.startThread()
    }
    private fun initSmartRefreshLayout() {
        SmartRefreshLayout.setDefaultRefreshHeaderCreator { context, layout ->
            layout.setPrimaryColorsId(R.color.my_bg, com.ioidea.base.R.color.text_color66) //全局设置主题颜色
            ClassicsHeader(context) //.setTimeFormat(new DynamicTimeFormat("更新于 %s"));//指定为经典Header，默认是 贝塞尔雷达Header
        }
        //设置全局的Footer构建器
        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreator { context, layout -> //指定为经典Footer，默认是 BallPulseFooter
            ClassicsFooter(context).setDrawableSize(20f)
        }
    }


    private fun initBle() {
        Ble.options()
            .setLogBleEnable(true) //设置是否输出打印蓝牙日志
            .setThrowBleException(true) //设置是否抛出蓝牙异常
            .setLogTAG("AndroidBLE") //设置全局蓝牙操作日志TAG
            .setAutoConnect(false) //设置是否自动连接
            .setIgnoreRepeat(false) //设置是否过滤扫描到的设备(已扫描到的不会再次扫描)
            .setConnectFailedRetryCount(5) //连接异常时（如蓝牙协议栈错误）,重新连接次数
            .setConnectTimeout((10 * 1000).toLong()) //设置连接超时时长
            .setScanPeriod((12 * 1000).toLong()) //设置扫描时长
            .setMaxConnectNum(7) //最大连接数量
            .setScanFilter(ScanFilter.Builder()
                .setServiceUuid(ParcelUuid.fromString(UuidUtils.uuid16To128("0001")))
                .build())
            .setUuidService(UUID.fromString(UuidUtils.uuid16To128("0001"))) //设置主服务的uuid
            .setUuidWriteCha(UUID.fromString(UuidUtils.uuid16To128("0002"))) //设置可写特征的uuid
            .setUuidReadCha(UUID.fromString(UuidUtils.uuid16To128("fd02"))) //设置可读特征的uuid （选填）
            .setUuidNotifyCha(UUID.fromString(UuidUtils.uuid16To128("0003"))) //设置可通知特征的uuid （选填，库中默认已匹配可通知特征的uuid）
            .setFactory(object : BleFactory<BleRssiDevice>() {
                //实现自定义BleDevice时必须设置
                override fun create(address: String?, name: String?): BleRssiDevice {
                    var addressHand=address
                    var nameHand=name
                    if(addressHand.isNullOrEmpty()){
                        addressHand=""
                    }
                    if(nameHand.isNullOrEmpty()){
                        nameHand=""
                    }
                    return BleRssiDevice(
                        addressHand,
                        nameHand
                    )
                }
            })
            .setBleWrapperCallback(MyBleWrapperCallback())
            .create<BleDevice>(
               this,
                object : InitCallback {
                    override fun success() {
                        BleLog.e("MainApplication", "初始化成功")
                    }

                    override fun failed(failedCode: Int) {
                        BleLog.e("MainApplication", "初始化失败：$failedCode")
                    }
                })
    }

}